DANCING MACHINE
Xorshift的なアルゴリズムを使った疑似乱数生成器を作成してロボットをダンスさせるステージ。
https://gyazo.com/b03534018f4c943232c3dbfc72d8534d
code:foo
seed = 29
code:疑似コード
temp1 = seed xor (seed shr 1)
temp2 = temp1 xor (temp1 shl 1)
next_seed = temp2 xor (temp2 shr 2)
out = next_seed mod 4
code:dancing_machine
const add 0
const add_ri 64
const add_ii 192
const sub 1
const sub_ri 65
const sub_ir 129
const and 2
const and_ri 66
const xor 5
const xor_ri 69
const shl 6
const shr 7
const jeq 32
const jeq_ri 96
const jeq_ii 224
const jlt 34
const jlte 35
const jgt 36
const jgte 37
const jgte_ri 101
const sw 16
const sw_ri 80
const lw 17
const lw_ri 81
const call 8
const ret 9
const r0 0
const r1 1
const r2 2
const r3 3
const r4 4
const r5 5
const sp 5
const sp_offset 4
const counter 6
const input 7
const output 7
const _ 0
const right 0
const down 1
const left 2
const up 3
const seed 1
const temp1 2
const temp2 3
const next_seed 4
const val 1
add input _ seed
label main
# temp1 = seed or (seed shr 1)
shr seed _ temp1
xor seed temp1 temp1
# temp2 = temp1 xor (temp1 shl 1)
shl temp1 _ temp2
xor temp1 temp2 temp2
# next_seed = temp2 xor (temp2 shr 2)
shr temp2 _ next_seed
shr next_seed _ next_seed
xor temp2 next_seed next_seed
# val = next_seed % 4
and_ri next_seed 3 val
add val _ output
add next_seed _ seed
jeq _ _ main
方向
0: 右
1: 下
2: 左
3: 上
Rubyで乱数を生成
code:dancing_machine.rb
# ruby dancing_machine.rb
def rand(seed)
temp1 = seed ^ (seed >> 1)
temp2 = temp1 ^ ((temp1 << 1) & 0b11111111)
next_seed = temp2 ^ (temp2 >> 2)
end
seed = 29
10.times do
next_seed = rand(seed)
seed = next_seed
end